//
//  RegistViewController.m
//  SIPphone
//
//  Created by wrt on 15-2-3.
//  Copyright (c) 2015年 wrtsoft. All rights reserved.
//

#import "RegistViewController.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "SVProgressHUD.h"

@interface RegistViewController ()
{
    int countNum;
}
@end

@implementation RegistViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NetPostClass shareNetPostClass] setDelegate:self];
    self.errorStatusLabel.text = @"";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //判断版本号 然后选择相应的ui界面
    float systemVersion = iOSValue;
    if (systemVersion >= 7.0)
    {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    [self.scrollView contentSizeToFit];
    
    self.title = NSLocalizedString(@"register account", @"注册账号 Register an account");
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@" %@",NSLocalizedString(@"return", @"返回 Return")] style:UIBarButtonItemStylePlain target:self action:@selector(returnButtonPressed)];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}

- (void)returnButtonPressed
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)checkCodeGetButtonPressed:(id)sender
{
    if (self.userNameTextField.text.length > 0)
    {
        [[NetPostClass shareNetPostClass] postGetCheckCodeWithUserName:self.userNameTextField.text];
        countNum = 120;
        [self.checkCodeButton setEnabled:NO];
        self.checkCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeCodeTime) userInfo:nil repeats:YES];
    }
    else
    {
        self.errorStatusLabel.text = NSLocalizedString(@"please fill in the phone number and then get verification code", @"请填写手机号后再获取验证码 Please fill in the phone number and then get verification code");
    }
}

- (void)changeCodeTime
{
    countNum--;
    if (countNum < 1)
    {
        [self.checkCodeTimer invalidate];
        self.timeLabel.text = @"120";
        [self.checkCodeButton setEnabled:YES];
        return;
    }
    self.timeLabel.text = [NSString stringWithFormat:@"%d",countNum];
}

- (IBAction)finishRegistButtonPressed:(id)sender
{
    if (self.userNameTextField.text.length > 0 && self.passwordTextField.text.length > 0 && self.checkPasswordTextField.text.length > 0 && self.checkCodeTextField.text.length > 0)
    {
        if ([self.passwordTextField.text isEqualToString:self.checkPasswordTextField.text])
        {
            [[NetPostClass shareNetPostClass] postRegistInfoWithUserName:self.userNameTextField.text andPassWord:self.checkPasswordTextField.text andUserCode:nil andName:nil andSex:nil andCredentialsTypeId:nil andCredentialsNum:nil andBirthday:nil andEmail:nil andRegisterTime:nil andRemark:nil andAddress:nil andTelePhone:nil andVerification:self.checkCodeTextField.text];
        }
        else
        {
            self.errorStatusLabel.text = NSLocalizedString(@"are not the same password twice, and then check the registration operation", @"两次密码不相同，请检查后再进行注册操作 Are not the same password twice, and then check the registration operation");
        }
    }
    else
    {
        self.errorStatusLabel.text = NSLocalizedString(@"please fill in the following information then complete the registration operation", @"请将信息填写完整之后再进行注册操作 Please fill in the following information then complete the registration operation");
    }
}

#pragma NetPostDelegateMethod
/**
 *  post数据成功回调
 *
 *  @param comeBackDic 返回来的字典数据
 *  @param tagNumber   post的tag标识
 */
- (void)postSuccessfulWithString:(NSDictionary *)comeBackDic withTag:(int)tagNumber
{
    if (tagNumber == 4)
    {
        //post成功返回数据
        [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"sent successfully", @"发送成功 Sent successfully")];
        self.errorStatusLabel.text = @"";
    }
    else
    {
        //post成功返回数据
        [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"successful registration", @"注册成功 Successful registration")];
        [self dismissModalViewControllerAnimated:YES];
    }
}
/**
 *  post失败返回数据
 *
 *  @param faileString 失败的原因
 *  @param tagNumber   post的tag标识
 */
- (void)postFailedWithString:(NSString *)faileString withTag:(int)tagNumber
{
    //post失败返回的失败原因
    //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" message:faileString delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
    //    [alert show];
    
    if (tagNumber == 4)
    {
        self.errorStatusLabel.text = NSLocalizedString(@"get the code fails, check whether the network is normal", @"获取验证码失败，请检查网络是否正常 Get the code fails, check whether the network is normal");
    }
    else
    {
        self.errorStatusLabel.text = NSLocalizedString(@"registration fails, check whether the network is normal, and the information is correct", @"注册失败，请检查网络是否正常以及信息是否正确 Registration fails, check whether the network is normal, and the information is correct");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
